import { DataContextFactory } from './../domain/fbsDataContext';
import { Kitchensolution } from "../domain/model";
import { ServiceException } from "./serviceException";
import { Guid, Logger, ObjectCopier } from "../common";
import { Transaction } from "tiny-entity";

export class KitchensolutionService {
    private get ctx() {
        return DataContextFactory.GetDataContext();
    }
    constructor() {
    }

    async GetKitchensolutionById(id: string) {
        return await this.ctx.Kitchensolution.First(x => x.id == id, ['id'], [id]);
    }

    public async  queryKitchensolutions(query: { ids?: Array<string>, media: string }) {
        const kitchensolutions = await this.getAllKitchensolutions();
        let result;
        if (typeof query.ids != "undefined") {
            result = kitchensolutions.filter(x => query.ids.indexOf(x.id) > -1);
        }
        if (typeof query.media != "undefined") {
            result = kitchensolutions.filter(x => x.media == query.media)
        }
        return result;
    }


    /**
     * 获取厨显方案
     * 
     * @private
     * @returns
     * 
     * @memberOf KitchensolutionService
     */
    public async getAllKdsKitchensolutions() {
        const kitchensolutions = await this.getAllKitchensolutions();
        return kitchensolutions.filter(x => x.media == "kds")
    }

    public async getAllKitchensolutions() {
        const kitchensolutions = await this.ctx.Kitchensolution.ToList();
        const categories = await this.ctx.Category.ToList();
        categories.forEach(category => {
            delete category.parent;
        })
        const newKitchensolutionKp = { id: "all", name: "全部", media: "kds", categories, mode: "", store: {} };
        const newKitchensolution = { id: "all", name: "全部", media: "kp", categories, mode: "", store: {} };
        (<any>kitchensolutions).push(newKitchensolutionKp);
        (<any>kitchensolutions).push(newKitchensolution);
        return kitchensolutions;
    }

}